﻿using System;
using StockholmsStad.Data.Entity;
using UserRole=StockholmsStad.Runtime.Serialization.DataContract.Enums.UserRole;

namespace StockholmsStad.Data.ContractFactory
{
    /// <summary>
    /// Konvertering befintligt dataobjekt till kontraktsobjekt
    /// </summary>
    internal class UserFactory
    {
        public User CreateEntityObject(int? userId, string username, string password, int userRoleId)
        {
            var entityUser = new User
                                 {
                                     UserName = username,
                                     Password = password,
                                     UserRoleId = userRoleId,
                                 };

            if (userId.HasValue)
                entityUser.UserId = userId.Value;

            return entityUser;
        }

        public ParvisUser CreateEntityObject(Runtime.Serialization.DataContract.ParvisUser user)
        {
            var entityUser = new ParvisUser
                                 {
                                     FirstName = user.FirstName,
                                     LastName = user.LastName,
                                     PhoneNumber = user.PhoneNumber,
                                     EmailAddress = user.Username,
                                     UserRoleId = (int)user.Role,
                                     IsActive = user.IsActive,
                                 };

            if (user.ParvisUserId.HasValue)
                entityUser.ParvisUserId = user.ParvisUserId.Value;

            if (!string.IsNullOrEmpty(user.CellPhoneNumber))
                entityUser.CellPhoneNumber = user.CellPhoneNumber;

            return entityUser;
        }

        public Runtime.Serialization.DataContract.ParvisUser CreateContractObject(ParvisUser user)
        {
            var contractUser = new Runtime.Serialization.DataContract.ParvisUser
                                   {
                                       FirstName = user.FirstName,
                                       LastName = user.LastName,
                                       PhoneNumber = user.PhoneNumber,
                                       Username = user.EmailAddress,
                                       ParvisUserId = user.ParvisUserId,
                                       Role = (UserRole) Enum.ToObject(typeof (UserRole), user.UserRoleId),
                                       IsActive = user.IsActive
                                   };

            if (!string.IsNullOrEmpty(user.CellPhoneNumber))
                contractUser.CellPhoneNumber = user.CellPhoneNumber;

            return contractUser;
        }

        public Runtime.Serialization.DataContract.ParvisUser CreateContractObject(GetParvisUsersResult user)
        {

            var contractUser = new Runtime.Serialization.DataContract.ParvisUser
                                   {
                                       FirstName = user.FirstName,
                                       LastName = user.LastName,
                                       PhoneNumber = user.PhoneNumber,
                                       Username = user.EmailAddress,
                                       ParvisUserId = user.ParvisUserId,
                                       Role = (UserRole) Enum.ToObject(typeof (UserRole), user.UserRoleId),
                                       IsActive = user.IsActive,
                                   };

            if (!string.IsNullOrEmpty(user.CellPhoneNumber))
                contractUser.CellPhoneNumber = user.CellPhoneNumber;

            return contractUser;
        }

        public Runtime.Serialization.DataContract.User CreateContractObject(User user)
        {
            var contractUser = new Runtime.Serialization.DataContract.User
                                   {
                                       UserId = user.UserId,
                                       Username = user.UserName,
                                       Role = (UserRole) Enum.ToObject(typeof (UserRole), user.UserRoleId),
                                       
                                   };

            return contractUser;
        }

        public UserRole CreateContractObject(Entity.UserRole userrole)
        {
            return (UserRole)Enum.ToObject(typeof(UserRole), userrole.UserRoleId);
        }
    }
}